package itsm.isperp.module.controller.app;

import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.data.domain.QueryFilter;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilter;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppDict;
import itsm.isperp.module.entity.app.AppSettingConfig;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.app.AppSettingConfigService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 数据字典控制器
 * 
 * @author lizx
 * @date 2013-2-16
 * @version V1.0
 */
@Controller
@RequestMapping("app/dict")
public class AppDictController extends BaseController<AppDict> {

	/**
	 * 数据字典管理服务
	 */
	@Autowired
	protected AppDictService appDictService;

	@Autowired
	protected AppSettingConfigService appSettingConfigService;

	@Override
	public AppDictService getService() {
		return appDictService;
	}

	@Override
	public String getPageTitle() {
		return "系统字典";
	}

	/**
	 * 菜单管理界面
	 * 
	 * @return 菜单管理界面视图
	 */
	@RequestMapping(value = { "manage", "" })
	@Override
	public ModelAndView manage(HttpServletRequest req) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("pageTitle", this.getPageTitle());

		List<AppDict> list = this.getService().findAllType();

		mv.addObject("dictTypes", list);
		mv.setViewName(this.getModulePath() + "/manage");
		return mv;
	}

	/**
	 * 编辑界面
	 * 
	 * @param id
	 *            唯一标识
	 * @return 编辑视图
	 */
	@RequestMapping("editWithTypeCode")
	public ModelAndView editWithTypeCode(String id, HttpServletRequest req)
			throws Exception {
		AppDict dict = null;
		String typeCode = req.getParameter("typeCode");
		if (StringUtils.isEmpty(id)) {
			dict = new AppDict();
			dict.setId(HibernateUUIDGenerator.generate());

			if (!StringUtils.isEmpty(typeCode)) {
				AppSettingConfig sc = this.appSettingConfigService
						.getByCode(typeCode);
				dict.setDescription(sc == null ? null : sc.getName());
				dict.setTypeCode(typeCode);

				QueryFilter filter = new JqGridFilter();
				filter.addRule("typeCode", JqGridFilterRuleOp.eq, typeCode);
				int count = this.getService().findAllCount(filter).intValue();
				dict.setOrderNum(count + 1);
				dict.setEnable(true);
			}
		} else {
			dict = this.getService().findOne(id);
		}

		ModelAndView mv = new ModelAndView("app/dict/editWithTypeCode");

		mv.addObject("model", dict);
		mv.addObject("pageTitle", this.getPageTitle());
		return mv;
	}

	@RequestMapping("config/{typeCode}")
	public ModelAndView config(@PathVariable("typeCode") String typeCode) {
		ModelAndView mv = new ModelAndView("app/dict/config");
		mv.addObject("typeCode", typeCode);
		return mv;
	}

	@RequestMapping("listByType/{typeCode}")
	@ResponseBody
	public PageResponse<AppDict> listByType(
			@PathVariable("typeCode") String typeCode, JqGridRequest req) {

		req.isPageable(false).addFilterRule("type_code", JqGridFilterRuleOp.eq,
				typeCode);
		req.setSort("order_num", Direction.ASC);
		return this.getService().findAll(req).toPage();
	}
}
